﻿using System;
using System.Collections.Generic;
using System.Text;

namespace System.Reflection
{
    internal delegate object DynamicConstructorInfoHandler(object[] parameters);

	internal class DynamicConstructorInfo : IDynamicConstructorInfo
    {
        Type type = null;
        ConstructorInfo info;
        DynamicConstructorInfoHandler handler = null;

        public DynamicConstructorInfo(Type type, ConstructorInfo info)
        {
            this.type = type;
            this.info = info;
        }

        public object Invoke(object[] parameters)
        {
            int key = info.MetadataToken;

            if (handler != null)
            {
                return handler(parameters);
            }

            if (DynamicCacheFactory<DynamicConstructorInfoHandler>.Instance.Contains(key))
            {
                this.handler = DynamicCacheFactory<DynamicConstructorInfoHandler>.Instance.GetValue(key);
            }
            else
            {
                this.handler = DynamicMethodFactory.CreateDynamicConstructorInfoHandler(type, info);

                DynamicCacheFactory<DynamicConstructorInfoHandler>.Instance.AddValue(key, this.handler);
            }

            return this.handler(parameters);
        }
    }
}
